package com.andf.common.service.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.andf.basic.AndfSqlMapClientDao;
import com.andf.common.BaseForm;
import com.andf.common.BaseModel;
import com.andf.common.CloneObjcetBean;
import com.andf.common.service.IGenericService;
import com.andf.common.util.SqlMapConfig;
/**
 * @Title: getOperateId
 * @author lilubing
 * @Description: TODO()
 * @date 2013-4-21 上午10:51:21
 */
@SuppressWarnings("unchecked")
public class BasicServiceImpl extends AndfSqlMapClientDao implements IGenericService{

	public String getOperateId(String type, Class formClazz) {

		SqlMapConfig sqlMapConfig = ((SqlMapConfig) formClazz
				.getAnnotation(SqlMapConfig.class));

		String operateId = "";
		try {
			operateId = (String) sqlMapConfig.getClass().getMethod(type,
					new Class[] {}).invoke(sqlMapConfig, new Object[] {});
		} catch (Exception e) {
			e.printStackTrace();
		}

		String namesSpace = sqlMapConfig.nameSpace();

		if (StringUtils.isNotBlank(namesSpace)) {
			operateId = namesSpace + "." + operateId;
		}
		return operateId;
	}

	@Override
	public Object deleteModelListUseTran(Class formClazz, String[] ids) throws Exception {
		int sum = 0;
		try {
			beginTransaction();
			for (String id : ids) {
				getSqlSession().update(getOperateId("deleteId", formClazz), id);
			}
		} catch (Exception e) {
			sum = 0;
			rollbackTransaction();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			commitTransaction();
		}
		return sum;
	}

	@Override
	public Object deleteModelUseTran(Class formClazz, BaseForm baseForm) throws Exception {
		int sum = 0;
		try {
			beginTransaction();
			sum = getSqlSession().update(getOperateId("deleteId", formClazz), baseForm);
		} catch (Exception e) {
			sum = 0;
			rollbackTransaction();
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			commitTransaction();
		}
		return sum;
	}

	@Override
	public Object getOneModel(Class formClazz, BaseForm baseForm) {
		return getSqlSession().selectOne(getOperateId("selectOneId", formClazz),
				baseForm);
	}

	@Override
	public List getModelAll(Class formClazz, BaseForm baseForm) {
		return getSqlSession().selectList(getOperateId("selectId", formClazz),
				baseForm);
	}

	@Override
	public Object saveModel(Class formClazz, BaseForm baseForm) throws Exception{
		Object o = null;
		try {
			o = formClazz.newInstance();
			BeanUtils.copyProperties(o, baseForm);
		} catch (Exception e) {
			e.printStackTrace();
		}
		getSqlSession().insert(getOperateId("insertId", formClazz), o);
		BaseModel b = null;
			b = (BaseModel) CloneObjcetBean.clone(o, BaseModel.class);
		return b;
	}

	@Override
	public Object updateModel(Class formClazz, BaseForm baseForm) throws Exception {
		int result = 0;
		Object o = null;
		try {
			o = formClazz.newInstance();
			BeanUtils.copyProperties(o, baseForm);
			beginTransaction();
			result = getSqlSession().update(getOperateId("updateId", formClazz), o);
		} catch (Exception e) {
			rollbackTransaction();
			throw new Exception(e);
		} finally {
			commitTransaction();
		}
		return result;
	}

}
